import pygtk
pygtk.require('2.0')
import gtk
import sound
import os
import time

TYPING_ROOT = os.path.dirname(os.path.abspath(__file__))
while(os.path.isfile(TYPING_ROOT)):
    TYPING_ROOT = os.path.dirname(TYPING_ROOT)
IMAGE_SOURCE = TYPING_ROOT + os.sep + "images" + os.sep + "keyboard"

KEY_X = 600
KEY_Y = 250

class Typing(gtk.EventBox):

    def __init__(self, EBox):
        super(Typing, self).__init__()
        
        EBox.connect("key-press-event", self.keypresscall)       

        #Load up our keypress images for each letter, for easy access later
        self.dict_pixbuf = {}
        alphastring = "abcdefghijklmnopqrstuvwxyz"
        
        for i in range(len(alphastring)):
            letter = alphastring[i]
            self.dict_pixbuf[letter] = gtk.gdk.pixbuf_new_from_file(IMAGE_SOURCE + os.sep + "keypress" + os.sep + letter.upper() + ".gif").scale_simple(KEY_X, KEY_Y, gtk.gdk.INTERP_BILINEAR)
        
        self.dict_pixbuf[" "] = gtk.gdk.pixbuf_new_from_file(IMAGE_SOURCE + os.sep + "keypress" + os.sep + "space.gif").scale_simple(KEY_X, KEY_Y, gtk.gdk.INTERP_BILINEAR)
        self.keyispressed = False
        self.lastpressed = ""
        
        # Need to make this adjustable, not hard coded scaling?
        self.keyboardimage = gtk.TextView()
        self.keyboardimage.set_cursor_visible(False)
        self.keyboardimage.set_justification(gtk.JUSTIFY_CENTER)
        self.keyboardimage.set_editable(False)
        
        basepixbuf = gtk.gdk.pixbuf_new_from_file(IMAGE_SOURCE + os.sep + "keypress" + os.sep + "idle.gif").scale_simple(KEY_X, KEY_Y, gtk.gdk.INTERP_BILINEAR)
        self.dict_pixbuf["base"] = basepixbuf
        self.highlightkey("base")
        
        self.keyboardimage.show()

        #Create a frame for the keyboard, and attach it.        
        self.keyboardframe = gtk.Frame()
        self.keyboardframe.add(self.keyboardimage)
        self.keyboardframe.show()
        self.add(self.keyboardframe)

    #Loads up the image of a particular key highlighted.        
    def highlightkey(self, letter):
        if (self.dict_pixbuf.has_key(letter)):
            buffer = self.keyboardimage.get_buffer()
            buffer.set_text("\n\n")
            buffer.insert_pixbuf(buffer.get_end_iter(), self.dict_pixbuf[letter])
            self.keyboardimage.show()

    #Called whenever the user presses a key, to modify the keyboard appropriately            
    def keypresscall(self, widget, event):
        print "hearing keypress call %d" %event.keyval
        
        if (self.keyispressed == False):
            self.highlightkey("base")
            while gtk.events_pending():
                gtk.main_iteration(False)
            time.sleep(.005)

        self.keyispressed = True

        #If we have a valid letter, highlight that letter        
        if (event.keyval <= 122 and event.keyval >= 97):
            letter = event.string
            if (letter != self.lastpressed):
                self.highlightkey(letter)
                self.lastpressed = letter
        #Highlight spacebar if we got that, too
        elif (event.keyval == 32):
            self.highlightkey(" ")


def main():
    return 0       

if __name__ == "__main__":
    main()